package cn.nlsde.service.impl;

import cn.nlsde.constant.Constants;
import cn.nlsde.dao.TemplateDetailMapper;
import cn.nlsde.dao.TemplateMapper;
import cn.nlsde.entity.Template;
import cn.nlsde.service.TemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by zwj on 2016/10/6.
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    public final Logger logger = LoggerFactory.getLogger(TemplateServiceImpl.class);


    @Autowired
    TemplateMapper templateMapper;

    @Autowired
    TemplateDetailMapper templateDetailMapper;

    @Override
    public int save(Template obj) {
        return templateMapper.insert(obj);
    }

    @Override
    public int delete(Template obj) {
        return 0;
    }

    @Override
    public int update(Template obj) {
        return templateMapper.updateByPrimaryKey(obj);
    }

    @Override
    public Template selectById(Integer id) {
        return templateMapper.selectById(id);
    }

    @Override
    public List<Template> selectAll() {
        return templateMapper.selectAll();
    }

    @Override
    public int deleteById(Integer id) {
        return templateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Template> selectWithListPage(PageInfo<Template> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Template> weights = templateMapper.selectWithListPage();
        return new PageInfo<>(weights);
    }

    @Override
    public byte[] getTemplateById(Integer templateId) {
        List<String> categoryByTemplated = templateDetailMapper.getCategoryByTemplated(templateId);
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(20);
        sheet.setDefaultRowHeightInPoints(20);
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue(Constants.COMPANY_TITITLE);
        for (int i = 1; i <= categoryByTemplated.size(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(categoryByTemplated.get(i - 1));
        }

        ByteArrayOutputStream bps = new ByteArrayOutputStream();
        try {
            workbook.write(bps);
        } catch (IOException e) {
            logger.info("{}", e.getMessage());
        }
        return bps.toByteArray();
    }

    @Override
    public int selectCount() {
        return templateMapper.selectCount();
    }

    @Override
    public String getTemplateNameById(Integer templateId) {
        return templateMapper.getNameById(templateId);
    }
}
